﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class RFOutboundBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInventoryMgtDA InventoryMgtDA = DataAccess.CreateInventoryMgtDA();
        private static readonly IInvLotLocLpnDA invLotLocLpnDA = DataAccess.CreateInvLotLocLpnDA();
        private static readonly ILoadDetailDA loadDetailDA = DataAccess.CreateLoadDetailDA();
        private static readonly ILoadHeaderDA loadHeaderDA = DataAccess.CreateLoadHeaderDA();
        private static readonly ILoadPickDetailDA loadPickDetailDA = DataAccess.CreateLoadPickDetailDA();
        private static readonly ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private static readonly IPickDetailDA pickDetailDA = DataAccess.CreatePickDetailDA();
        private static readonly IPreallocatePickDetailDA preallocatePickDetailDA = DataAccess.CreatePreallocatePickDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRFOutboundDA rfOutboundDA = DataAccess.CreateRFOutboundDA();
        private static readonly IRotationRuleDA rotataionRuleDA = DataAccess.CreateRotationRuleDA();
        private static readonly IRotationRuleDetailDA rotataionRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly IVanningDA vanningDA = DataAccess.CreateVanningDA();
        private static readonly IVanningDetailDA vanningDetailDA = DataAccess.CreateVanningDetailDA();
        private static readonly IWaveDA waveDA = DataAccess.CreateWaveDA();
        private static readonly IWaveDetailDA waveDetailDA = DataAccess.CreateWaveDetailDA();
        private string whLoginID;

        public RFOutboundBLL()
        {
            this.whLoginID = string.Empty;
            this.pubParasInfo = null;
        }

        public RFOutboundBLL(string currentWH)
        {
            this.whLoginID = string.Empty;
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                this.whLoginID = this.dataBaseID[0];
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public RFOutboundBLL(PubParasInfo tempPubParasInfo)
        {
            this.whLoginID = string.Empty;
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                this.whLoginID = this.dataBaseID[0];
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string BatchPickingByWave(string waveID, string lpnID)
        {
            string result = string.Empty;
            string errorInfo = string.Empty;
            bool skuExist = false;
            bool skuShipped = false;
            bool match = false;
            bool matchAttr01Info = true;
            bool matchAttr09Info = true;
            List<OrderDetailInfo> orderDetailListPick = new List<OrderDetailInfo>();
            if (!string.IsNullOrEmpty(waveID) && !string.IsNullOrEmpty(lpnID))
            {
                InventoryInfo inventoryInfo = InventoryMgtDA.GetInventoryInfoByLpnID(lpnID, this.whLoginID);
                List<WaveDetailInfo> waveDetailInfoList = null;
                if (inventoryInfo != null)
                {
                    int j;
                    decimal lpnQty = ((((inventoryInfo.Qty - inventoryInfo.AllocatedQty) - inventoryInfo.ArchiveQty) - inventoryInfo.ExpectedQty) - inventoryInfo.PendingMoveIn) - inventoryInfo.PickedQty;
                    if (lpnQty <= 0M)
                    {
                        throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("LPN库存记录可用数量为0;LPN庫存記錄可用數量爲0;LPN inventory available quantity is 0", this.pubParasInfo.UserLanguage));
                    }
                    waveDetailInfoList = waveDetailDA.GetWaveDetailListByID(waveID, this.whLoginID);
                    if ((waveDetailInfoList == null) || (waveDetailInfoList.Count <= 0))
                    {
                        throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("批量订单没有明细记录;批量訂單沒有明細記錄;Batch order is empty", this.pubParasInfo.UserLanguage));
                    }
                    OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL(this.pubParasInfo);
                    for (int i = 0; i < waveDetailInfoList.Count; i++)
                    {
                        if (waveDetailInfoList[i].ProcessStatus.ToUpper() != "SHIPPED")
                        {
                            string where = " AND OD.OWNER_ID = '" + inventoryInfo.OwnerID + "' AND OD.SKU_ID = '" + inventoryInfo.SkuID + "' AND OD.STATUS IN ('New','PartPicked','Picked','PartShipped','Shipped') ";
                            List<OrderDetailInfo> orderDetailList = orderDetailDA.GetOrderDetailListByOrderID(waveDetailInfoList[i].OrderID, where, this.whLoginID, this.pubParasInfo);
                            j = 0;
                            while (j < orderDetailList.Count)
                            {
                                if (orderDetailList[j].Status.ToUpper() != "SHIPPED")
                                {
                                    if ((!string.IsNullOrEmpty(orderDetailList[j].LotAttr09) && (orderDetailList[j].LotAttr09.ToUpper() != inventoryInfo.LotAttr09.ToUpper())) || (!string.IsNullOrEmpty(orderDetailList[j].LotAttr01) && (orderDetailList[j].LotAttr01.ToUpper() != inventoryInfo.LotAttr01.ToUpper())))
                                    {
                                        if (!(string.IsNullOrEmpty(orderDetailList[j].LotAttr09) || !(orderDetailList[j].LotAttr09.ToUpper() != inventoryInfo.LotAttr09.ToUpper())))
                                        {
                                            matchAttr09Info = false;
                                        }
                                        if (!(string.IsNullOrEmpty(orderDetailList[j].LotAttr01) || !(orderDetailList[j].LotAttr01.ToUpper() != inventoryInfo.LotAttr01.ToUpper())))
                                        {
                                            matchAttr01Info = false;
                                        }
                                    }
                                    else
                                    {
                                        orderDetailListPick.Add(orderDetailList[j]);
                                        match = true;
                                    }
                                    skuExist = true;
                                }
                                else
                                {
                                    skuShipped = true;
                                }
                                j++;
                            }
                            if ((orderDetailList == null) || (orderDetailList.Count == 0))
                            {
                                match = true;
                            }
                            orderDetailList.Clear();
                        }
                    }
                    if ((orderDetailListPick != null) && (orderDetailListPick.Count > 0))
                    {
                        skuShipped = false;
                        for (j = 0; j < orderDetailListPick.Count; j++)
                        {
                            if (lpnQty == 0M)
                            {
                                break;
                            }
                            if ((j == (orderDetailListPick.Count - 1)) || ((j < (orderDetailListPick.Count - 1)) && (orderDetailListPick[j].Status.ToUpper() != "PICKED")))
                            {
                                PickDetailInfo pickDetaiInfo = new PickDetailInfo {
                                    Lot = inventoryInfo.Lot,
                                    Loc = inventoryInfo.Loc,
                                    LpnID = inventoryInfo.LpnID,
                                    OrderID = orderDetailListPick[j].OrderID,
                                    OrderLineID = orderDetailListPick[j].LineID,
                                    OwnerID = orderDetailListPick[j].OwnerID,
                                    PackID = orderDetailListPick[j].PackID,
                                    SkuID = orderDetailListPick[j].SkuID,
                                    Status = "New",
                                    UomID = orderDetailListPick[j].UomID,
                                    CaseID = "",
                                    AltSkuID = "",
                                    DropID = "",
                                    UomQty = 0M,
                                    MovedQty = 0M,
                                    UpdateSource = "",
                                    PickMethod = "",
                                    WaveID = "",
                                    FreightCharges = 0.0,
                                    IsClosed = "",
                                    QcStatus = "",
                                    CreatedBy = this.pubParasInfo.UserID,
                                    UpdatedBy = this.pubParasInfo.UserID,
                                    EffectiveDate = DateTime.Now
                                };
                                if ((orderDetailListPick[j].OriginalQty - orderDetailListPick[j].PickedQty) >= lpnQty)
                                {
                                    pickDetaiInfo.Qty = lpnQty;
                                    lpnQty = 0M;
                                }
                                else if (j == (orderDetailListPick.Count - 1))
                                {
                                    pickDetaiInfo.Qty = lpnQty;
                                    lpnQty = 0M;
                                }
                                else
                                {
                                    pickDetaiInfo.Qty = orderDetailListPick[j].OriginalQty - orderDetailListPick[j].PickedQty;
                                    lpnQty -= pickDetaiInfo.Qty;
                                }
                                if (pickDetaiInfo.Qty > 0M)
                                {
                                    string pickDetailID = outBoundMgtBll.ManualAllocate(new List<PickDetailInfo> { pickDetaiInfo });
                                    if (!string.IsNullOrEmpty(pickDetailID))
                                    {
                                        result = result + outBoundMgtBll.ConfirmPicked(new List<string> { pickDetailID }, new List<string> { "PickTo" }, this.pubParasInfo.UserID) + ",";
                                    }
                                }
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(result))
                    {
                        if (!match)
                        {
                            if (!matchAttr01Info)
                            {
                                errorInfo = PublicMethod.GetInstance().RF_ShowAlertMessage("[版本号不正确];[版本號不正確];LotAttr01 is not match", this.pubParasInfo.UserLanguage);
                            }
                            if (!matchAttr09Info)
                            {
                                errorInfo = errorInfo + PublicMethod.GetInstance().RF_ShowAlertMessage("[货品属性不正确];[貨品屬性不正確];LotAttr09 is not match", this.pubParasInfo.UserLanguage);
                            }
                            if (!string.IsNullOrEmpty(errorInfo))
                            {
                                throw new Exception(errorInfo);
                            }
                        }
                        if (!skuExist)
                        {
                            throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("订单不存在此货品;訂單不存在此貨品;SKU is not exist in this Pick Order", this.pubParasInfo.UserLanguage));
                        }
                        if (skuShipped)
                        {
                            throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("LPN已发货;LPN已發貨;LPN is already Shipped", this.pubParasInfo.UserLanguage));
                        }
                    }
                }
                WaveBLL waveBll = new WaveBLL(this.pubParasInfo);
                waveDetailInfoList = waveDetailDA.GetWaveDetailListByID(waveID, this.whLoginID);
                string waveStatus = waveBll.GetWaveStatus(waveDetailInfoList);
                waveDA.UpdateWaveStatus(waveID, waveStatus, this.pubParasInfo.UserID, this.whLoginID);
            }
            return result;
        }

        public void BatchShippingByWave(string waveID)
        {
            new WaveBLL(this.pubParasInfo).BatchShipping(waveID);
        }

        public string GeneratePickingByLpn(string orderID, string lpnID)
        {
            string result = string.Empty;
            string errorInfo = string.Empty;
            bool skuShipped = false;
            bool match = false;
            bool matchAttr01Info = true;
            bool matchAttr09Info = true;
            List<OrderDetailInfo> orderDetailListPick = new List<OrderDetailInfo>();
            if (!string.IsNullOrEmpty(orderID) && !string.IsNullOrEmpty(lpnID))
            {
                int i;
                InventoryInfo inventoryInfo = InventoryMgtDA.GetInventoryInfoByLpnID(lpnID, this.whLoginID);
                if (inventoryInfo == null)
                {
                    throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("未找到相对应库存;未找到相對應庫存;Inventory is not exist", this.pubParasInfo.UserLanguage));
                }
                string where = " AND OD.OWNER_ID = '" + inventoryInfo.OwnerID + "' AND OD.SKU_ID = '" + inventoryInfo.SkuID + "' AND OD.STATUS IN ('New','PartPicked','Picked','PartShipped','Shipped') ";
                List<OrderDetailInfo> orderDetailList = orderDetailDA.GetOrderDetailListByOrderID(orderID, where, this.whLoginID, this.pubParasInfo);
                if (orderDetailList.Count == 0)
                {
                    throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("订单不存在此货品;訂單不存在此貨品;SKU is not exist in this Pick Order", this.pubParasInfo.UserLanguage));
                }
                for (i = 0; i < orderDetailList.Count; i++)
                {
                    if (orderDetailList[i].Status.ToUpper() != "SHIPPED")
                    {
                        if ((!string.IsNullOrEmpty(orderDetailList[i].LotAttr09) && (orderDetailList[i].LotAttr09.ToUpper() != inventoryInfo.LotAttr09.ToUpper())) || (!string.IsNullOrEmpty(orderDetailList[i].LotAttr01) && (orderDetailList[i].LotAttr01.ToUpper() != inventoryInfo.LotAttr01.ToUpper())))
                        {
                            if (!(string.IsNullOrEmpty(orderDetailList[i].LotAttr09) || !(orderDetailList[i].LotAttr09.ToUpper() != inventoryInfo.LotAttr09.ToUpper())))
                            {
                                matchAttr09Info = false;
                            }
                            if (!(string.IsNullOrEmpty(orderDetailList[i].LotAttr01) || !(orderDetailList[i].LotAttr01.ToUpper() != inventoryInfo.LotAttr01.ToUpper())))
                            {
                                matchAttr01Info = false;
                            }
                        }
                        else
                        {
                            orderDetailListPick.Add(orderDetailList[i]);
                            match = true;
                        }
                    }
                    else
                    {
                        skuShipped = true;
                    }
                }
                if ((orderDetailList == null) || (orderDetailList.Count == 0))
                {
                    match = true;
                }
                orderDetailList.Clear();
                if (!match)
                {
                    if (!matchAttr01Info)
                    {
                        errorInfo = PublicMethod.GetInstance().RF_ShowAlertMessage("[版本号不正确];[版本號不正確];LotAttr01 is not match", this.pubParasInfo.UserLanguage);
                    }
                    if (!matchAttr09Info)
                    {
                        errorInfo = errorInfo + PublicMethod.GetInstance().RF_ShowAlertMessage("[货品属性不正确];[貨品屬性不正確];LotAttr09 is not match", this.pubParasInfo.UserLanguage);
                    }
                    if (!string.IsNullOrEmpty(errorInfo))
                    {
                        throw new Exception(errorInfo);
                    }
                }
                if (((orderDetailListPick == null) || (orderDetailListPick.Count == 0)) && skuShipped)
                {
                    throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("LPN已发货;LPN已發貨;LPN is already Shipped", this.pubParasInfo.UserLanguage));
                }
                OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL(this.pubParasInfo);
                if ((orderDetailListPick == null) || (orderDetailListPick.Count <= 0))
                {
                    return result;
                }
                decimal lpnQty = ((((inventoryInfo.Qty - inventoryInfo.AllocatedQty) - inventoryInfo.ArchiveQty) - inventoryInfo.ExpectedQty) - inventoryInfo.PendingMoveIn) - inventoryInfo.PickedQty;
                if (lpnQty <= 0M)
                {
                    throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("LPN库存记录可用数量为0;LPN庫存記錄可用數量爲0;LPN inventory available quantity is 0", this.pubParasInfo.UserLanguage));
                }
                for (i = 0; i < orderDetailListPick.Count; i++)
                {
                    if (lpnQty == 0M)
                    {
                        return result;
                    }
                    if ((i == (orderDetailListPick.Count - 1)) || ((i < (orderDetailListPick.Count - 1)) && (orderDetailListPick[i].Status.ToUpper() != "PICKED")))
                    {
                        PickDetailInfo pickDetaiInfo = new PickDetailInfo {
                            Lot = inventoryInfo.Lot,
                            Loc = inventoryInfo.Loc,
                            LpnID = inventoryInfo.LpnID,
                            OrderID = orderDetailListPick[i].OrderID,
                            OrderLineID = orderDetailListPick[i].LineID,
                            OwnerID = orderDetailListPick[i].OwnerID,
                            PackID = orderDetailListPick[i].PackID,
                            SkuID = orderDetailListPick[i].SkuID,
                            Status = "New",
                            UomID = orderDetailListPick[i].UomID,
                            CaseID = "",
                            AltSkuID = "",
                            DropID = "",
                            UomQty = 0M,
                            MovedQty = 0M,
                            UpdateSource = "",
                            PickMethod = "",
                            WaveID = "",
                            FreightCharges = 0.0,
                            IsClosed = "",
                            QcStatus = "",
                            CreatedBy = this.pubParasInfo.UserID,
                            UpdatedBy = this.pubParasInfo.UserID,
                            EffectiveDate = DateTime.Now,
                            Qty = inventoryInfo.Qty
                        };
                        if ((orderDetailListPick[i].OriginalQty - orderDetailListPick[i].PickedQty) >= lpnQty)
                        {
                            pickDetaiInfo.Qty = lpnQty;
                            lpnQty = 0M;
                        }
                        else if (i == (orderDetailListPick.Count - 1))
                        {
                            pickDetaiInfo.Qty = lpnQty;
                            lpnQty = 0M;
                        }
                        else
                        {
                            pickDetaiInfo.Qty = orderDetailListPick[i].OriginalQty - orderDetailListPick[i].PickedQty;
                            lpnQty -= pickDetaiInfo.Qty;
                        }
                        if (pickDetaiInfo.Qty > 0M)
                        {
                            string pickDetailID = outBoundMgtBll.ManualAllocate(new List<PickDetailInfo> { pickDetaiInfo });
                            if (!string.IsNullOrEmpty(pickDetailID))
                            {
                                result = outBoundMgtBll.ConfirmPicked(new List<string> { pickDetailID }, new List<string> { "PickTo" }, this.pubParasInfo.UserID);
                            }
                        }
                    }
                }
            }
            return result;
        }

        public string GeneratePickingByPickDetail(RFPickDetailInfo rfPickDetailInfo)
        {
            string result = string.Empty;
            string pickByDetail = string.Empty;
            string lpnID = string.Empty;
            string invLocLot = string.Empty;
            List<string> pickDetailIDS = new List<string>();
            List<string> toLocS = new List<string>();
            string orderID = rfPickDetailInfo.OrderID;
            string where = " WHERE LA.LOT_ATTR05 = '" + rfPickDetailInfo.LotAttr05 + "' AND PD.ORDER_ID = '" + orderID + "'";
            PickDetailInfo pickDetailInfo = pickDetailDA.GetPickDetailByWhere(where, this.whLoginID, this.pubParasInfo);
            if (pickDetailInfo != null)
            {
                if (pickDetailInfo.Status != "New")
                {
                    throw new Exception("拣货失败：序列号货品已经拣货");
                }
                if (pickDetailInfo.Loc.ToUpper() != rfPickDetailInfo.Loc.ToUpper())
                {
                    throw new Exception("拣货失败：拣货货位和序列号所在货位不一致");
                }
                LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(pickDetailInfo.Lot, this.whLoginID);
                if (lotAttributeInfo != null)
                {
                    if (lotAttributeInfo.SkuID != rfPickDetailInfo.SkuID)
                    {
                        throw new Exception("拣货失败：序列号所属货品和建议不一致");
                    }
                    if (lotAttributeInfo.ExternalLot.ToUpper() != rfPickDetailInfo.LotExternalLot.ToUpper())
                    {
                        throw new Exception("拣货失败：序列号所属货品批号和建议不一致");
                    }
                }
                pickDetailIDS.Add(pickDetailInfo.PickDetailID);
                if (string.IsNullOrEmpty(pickDetailInfo.ToLoc))
                {
                    pickDetailInfo.ToLoc = "PickTo";
                }
                toLocS.Add(pickDetailInfo.ToLoc);
                pickByDetail = "Y";
            }
            else
            {
                string whereCurPick;
                List<InvLotLocLpnInfo> invLotLocLpnInfos = new List<InvLotLocLpnInfo>();
                ReceiptLotInfo lotAttribute = new ReceiptLotInfo {
                    LotAttr05 = rfPickDetailInfo.LotAttr05,
                    ExternalLot = rfPickDetailInfo.LotExternalLot
                };
                invLotLocLpnInfos = invLotLocLpnDA.GetInvLotLocLpnByLotAttribute(lotAttribute, rfPickDetailInfo.OwnerID, rfPickDetailInfo.SkuID, this.whLoginID);
                if ((invLotLocLpnInfos == null) || (invLotLocLpnInfos.Count <= 0))
                {
                    throw new Exception("扫描序列号有误，未找到合适库存！");
                }
                decimal qty = 0M;
                for (int i = 0; i < invLotLocLpnInfos.Count; i++)
                {
                    decimal otherQty = ((((invLotLocLpnInfos[i].AllocatedQty + invLotLocLpnInfos[i].PickedQty) + invLotLocLpnInfos[i].PendingMoveIn) + invLotLocLpnInfos[i].PickinprocessQty) + invLotLocLpnInfos[i].ExpectedQty) + invLotLocLpnInfos[i].ArchiveQty;
                    decimal availQty = invLotLocLpnInfos[i].Qty - otherQty;
                    if ((invLotLocLpnInfos[i].Qty > 0M) && (invLotLocLpnInfos[i].Loc.ToUpper() == rfPickDetailInfo.Loc.ToUpper()))
                    {
                        qty += invLotLocLpnInfos[i].Qty;
                        if (invLotLocLpnInfos[i].Qty == availQty)
                        {
                            lpnID = invLotLocLpnInfos[i].LpnID;
                            invLocLot = invLotLocLpnInfos[i].Lot;
                            break;
                        }
                    }
                }
                if (string.IsNullOrEmpty(invLocLot) && (qty > 0M))
                {
                    string whereOtherPick = string.Concat(new object[] { " WHERE LA.LOT_ATTR05 = '", rfPickDetailInfo.LotAttr05, "' AND PD.ORDER_ID <> '", orderID, "' AND PD.QTY = ", rfPickDetailInfo.Qty, " AND PD.OWNER_ID = '", rfPickDetailInfo.OwnerID, "' AND PD.SKU_ID = '", rfPickDetailInfo.SkuID, "'" });
                    PickDetailInfo pickDetailInfoOtherPick = pickDetailDA.GetPickDetailByWhere(whereOtherPick, this.whLoginID, this.pubParasInfo);
                    if (pickDetailInfoOtherPick == null)
                    {
                        throw new Exception("扫描序列号有误，未找到合适库存！");
                    }
                    PreallocatePickDetailInfo preallocatePickDetailInfoCurPick = null;
                    PreallocatePickDetailInfo preallocatePickDetailInfoOtherPick = null;
                    whereCurPick = " WHERE PD.STATUS = 'New' AND LA.EXTERNAL_LOT = '" + lotAttribute.ExternalLot + "' AND PD.ORDER_ID = '" + orderID + "' AND PD.OWNER_ID = '" + rfPickDetailInfo.OwnerID + "' AND PD.SKU_ID = '" + rfPickDetailInfo.SkuID + "' AND PD.LOC = '" + rfPickDetailInfo.Loc + "'";
                    PickDetailInfo pickDetailInfoCurPick = pickDetailDA.GetPickDetailByWhere(whereCurPick, this.whLoginID, this.pubParasInfo);
                    if (pickDetailInfoCurPick == null)
                    {
                        throw new Exception("未查找到相应拣货明细");
                    }
                    if (!string.IsNullOrEmpty(pickDetailInfoCurPick.PreallocatePickDetailID))
                    {
                        preallocatePickDetailInfoCurPick = preallocatePickDetailDA.GetPreallocatePickDetailByID(pickDetailInfoCurPick.PreallocatePickDetailID, this.whLoginID);
                    }
                    if (!string.IsNullOrEmpty(pickDetailInfoOtherPick.PreallocatePickDetailID))
                    {
                        preallocatePickDetailInfoOtherPick = preallocatePickDetailDA.GetPreallocatePickDetailByID(pickDetailInfoOtherPick.PreallocatePickDetailID, this.whLoginID);
                    }
                    if (preallocatePickDetailInfoCurPick != null)
                    {
                        if (preallocatePickDetailInfoOtherPick != null)
                        {
                            preallocatePickDetailInfoOtherPick.Lot = pickDetailInfoCurPick.Lot;
                            preallocatePickDetailInfoOtherPick.UpdatedBy = this.pubParasInfo.UserID;
                            preallocatePickDetailDA.UpdatePreallocatePickDetail(preallocatePickDetailInfoOtherPick, this.whLoginID);
                            preallocatePickDetailInfoCurPick.Lot = pickDetailInfoOtherPick.Lot;
                            preallocatePickDetailInfoCurPick.UpdatedBy = this.pubParasInfo.UserID;
                            preallocatePickDetailDA.UpdatePreallocatePickDetail(preallocatePickDetailInfoCurPick, this.whLoginID);
                        }
                        else
                        {
                            preallocatePickDetailInfoCurPick.Lot = pickDetailInfoOtherPick.Lot;
                            preallocatePickDetailDA.UpdatePreallocatePickDetail(preallocatePickDetailInfoCurPick, this.whLoginID);
                        }
                    }
                    else if (preallocatePickDetailInfoOtherPick != null)
                    {
                        preallocatePickDetailInfoOtherPick.Lot = pickDetailInfoCurPick.Lot;
                        preallocatePickDetailDA.UpdatePreallocatePickDetail(preallocatePickDetailInfoOtherPick, this.whLoginID);
                    }
                    string otherPickLot = pickDetailInfoOtherPick.Lot;
                    pickDetailInfoOtherPick.Lot = pickDetailInfoCurPick.Lot;
                    pickDetailInfoOtherPick.UpdatedBy = this.pubParasInfo.UserID;
                    pickDetailDA.UpdatePickDetail(pickDetailInfoOtherPick, this.whLoginID);
                    pickDetailInfoCurPick.Lot = otherPickLot;
                    pickDetailInfoCurPick.UpdatedBy = this.pubParasInfo.UserID;
                    pickDetailDA.UpdatePickDetail(pickDetailInfoCurPick, this.whLoginID);
                    pickByDetail = "Y";
                    pickDetailIDS.Add(pickDetailInfoCurPick.PickDetailID);
                    if (string.IsNullOrEmpty(pickDetailInfoCurPick.ToLoc))
                    {
                        pickDetailInfoCurPick.ToLoc = "PickTo";
                    }
                    toLocS.Add(pickDetailInfoCurPick.ToLoc);
                }
                else
                {
                    PickDetailInfo pickDetailInfo2 = pickDetailDA.GetPickDetailByID(rfPickDetailInfo.PickDetailID, this.whLoginID, this.pubParasInfo);
                    if (pickDetailInfo2 == null)
                    {
                        throw new Exception("单据发生变化，拣货失败！");
                    }
                    if (pickDetailInfo2.Status != "New")
                    {
                        string loc = rfPickDetailInfo.Loc;
                        string skuID = rfPickDetailInfo.SkuID;
                        whereCurPick = " WHERE PD.STATUS = 'New' AND LA.EXTERNAL_LOT = '" + lotAttribute.ExternalLot + "' AND PD.ORDER_ID = '" + orderID + "' AND PD.OWNER_ID = '" + rfPickDetailInfo.OwnerID + "' AND PD.SKU_ID = '" + rfPickDetailInfo.SkuID + "' AND PD.LOC = '" + rfPickDetailInfo.Loc + "'";
                        PickDetailInfo pickDetailInfo3 = pickDetailDA.GetPickDetailByWhere(whereCurPick, this.whLoginID, this.pubParasInfo);
                        if (pickDetailInfo3 == null)
                        {
                            throw new Exception("货位:" + loc + ",货品：" + skuID + "已拣货完毕！");
                        }
                        rfPickDetailInfo.PickDetailID = pickDetailInfo3.PickDetailID;
                        rfPickDetailInfo.OrderLineID = pickDetailInfo3.OrderLineID;
                    }
                }
            }
            OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL(this.pubParasInfo);
            if (pickByDetail == "Y")
            {
                if (pickDetailIDS.Count > 0)
                {
                    result = outBoundMgtBll.ConfirmPicked(pickDetailIDS, toLocS, this.pubParasInfo.UserID);
                }
                return result;
            }
            List<string> gInitLocal3 = new List<string> {
                rfPickDetailInfo.PickDetailID
            };
            if (!string.IsNullOrEmpty(outBoundMgtBll.RevertAllocate(gInitLocal3, this.pubParasInfo.UserID)))
            {
                outBoundMgtBll.RevertPreAllocate(orderID, new List<string> { rfPickDetailInfo.OrderLineID.ToString() }, this.pubParasInfo.UserID);
                PickDetailInfo pickDetaiInfo = new PickDetailInfo {
                    Lot = invLocLot,
                    Loc = rfPickDetailInfo.Loc,
                    LpnID = lpnID,
                    OrderID = rfPickDetailInfo.OrderID,
                    OrderLineID = rfPickDetailInfo.OrderLineID,
                    OwnerID = rfPickDetailInfo.OwnerID,
                    PackID = rfPickDetailInfo.PackID,
                    SkuID = rfPickDetailInfo.SkuID,
                    Status = "New",
                    UomID = rfPickDetailInfo.UomID,
                    CaseID = "",
                    AltSkuID = "",
                    DropID = "",
                    UomQty = 0M,
                    MovedQty = 0M,
                    UpdateSource = "",
                    PickMethod = "",
                    WaveID = "",
                    FreightCharges = 0.0,
                    IsClosed = "",
                    QcStatus = "",
                    CreatedBy = this.pubParasInfo.UserID,
                    UpdatedBy = this.pubParasInfo.UserID,
                    EffectiveDate = DateTime.Now,
                    Qty = rfPickDetailInfo.Qty
                };
                if (pickDetaiInfo.Qty <= 0M)
                {
                    return result;
                }
                string pickDetailID = outBoundMgtBll.ManualAllocate(new List<PickDetailInfo> { pickDetaiInfo });
                if (!string.IsNullOrEmpty(pickDetailID))
                {
                    result = outBoundMgtBll.ConfirmPicked(new List<string> { pickDetailID }, new List<string> { "PickTo" }, this.pubParasInfo.UserID);
                }
            }
            return result;
        }

        public string GeneratePickingByPickDetails(List<RFPickDetailInfo> rfPickDetailInfos)
        {
            int i;
            List<string> pickDetailIDS = new List<string>();
            List<string> toLocS = new List<string>();
            List<string> pickDetailQtyS = new List<string>();
            List<PickDetailInfo> pickDetailInfos = new List<PickDetailInfo>();
            int temp = 0;
            string pickMethod = string.Empty;
            string toLoc = string.Empty;
            decimal pickQty = 0M;
            decimal pickedQty = 0M;
            if ((rfPickDetailInfos != null) && (rfPickDetailInfos.Count > 0))
            {
                pickMethod = rfPickDetailInfos[0].PickMethod;
                toLoc = rfPickDetailInfos[0].ToLoc;
                pickQty = rfPickDetailInfos[0].Qty;
                for (i = 0; i < rfPickDetailInfos.Count; i++)
                {
                    PickDetailInfo pickDetailInfo = pickDetailDA.GetPickDetailByID(rfPickDetailInfos[i].PickDetailID, this.whLoginID, this.pubParasInfo);
                    if (pickDetailInfo.Status != "New")
                    {
                        if (pickMethod == "Y")
                        {
                            throw new Exception("分配明细发生变化，拣货失败");
                        }
                    }
                    else if (pickMethod == "Y")
                    {
                        pickDetailIDS.Add(rfPickDetailInfos[i].PickDetailID);
                        if (!string.IsNullOrEmpty(toLoc))
                        {
                            toLocS.Add(toLoc);
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(pickDetailInfo.ToLoc))
                            {
                                pickDetailInfo.ToLoc = "PickTo";
                            }
                            toLocS.Add(pickDetailInfo.ToLoc);
                        }
                    }
                    else
                    {
                        decimal lastQty = pickQty - pickedQty;
                        decimal diffQty = pickDetailInfo.Qty - pickDetailInfo.MovedQty;
                        if (lastQty >= diffQty)
                        {
                            pickedQty += diffQty;
                            pickDetailInfo.MovedQty += diffQty;
                            pickDetailInfos.Add(pickDetailInfo);
                        }
                        else
                        {
                            pickDetailInfo.MovedQty += lastQty;
                            pickDetailInfos.Add(pickDetailInfo);
                            pickedQty += lastQty;
                        }
                        if (pickDetailInfo.Qty == pickDetailInfo.MovedQty)
                        {
                            pickDetailIDS.Add(rfPickDetailInfos[i].PickDetailID);
                            if (!string.IsNullOrEmpty(toLoc))
                            {
                                toLocS.Add(toLoc);
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(pickDetailInfo.ToLoc))
                                {
                                    pickDetailInfo.ToLoc = "PickTo";
                                }
                                toLocS.Add(pickDetailInfo.ToLoc);
                            }
                        }
                        if (pickedQty == pickQty)
                        {
                            break;
                        }
                    }
                }
                if ((pickMethod != "Y") && (pickedQty != pickQty))
                {
                    throw new Exception("拣货失败：拣货数量大于分配数量");
                }
            }
            OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL(this.pubParasInfo);
            if (pickMethod != "Y")
            {
                for (i = 0; i < pickDetailInfos.Count; i++)
                {
                    if (pickDetailInfos[i].Qty == pickDetailInfos[i].MovedQty)
                    {
                        pickDetailInfos[i].MovedQty = 0M;
                    }
                    temp += pickDetailDA.UpdatePickDetail(pickDetailInfos[i], this.whLoginID);
                }
            }
            if (pickDetailIDS.Count > 0)
            {
                return outBoundMgtBll.ConfirmPicked(pickDetailIDS, toLocS, this.pubParasInfo.UserID);
            }
            return temp.ToString();
        }

        public string GenerateVanningDetailByPickDetails(string orderID, string orderType, string containerID, List<RFPickDetailInfo> rfPickDetailInfos)
        {
            VanningInfo vanningInfo;
            string where;
            int i;
            int result = 0;
            bool vanningExist = false;
            string pickMethod = string.Empty;
            decimal loadQty = 0M;
            decimal loadedQty = 0M;
            bool containerIDGenerate = false;
            if (orderType == "ORDER")
            {
                where = " WHERE ORDER_TYPE = 'ORDER'  AND ORDER_ID = '" + orderID + "'";
                vanningInfo = vanningDA.GetVanningByWhere(where, this.whLoginID, this.pubParasInfo);
            }
            else
            {
                where = " WHERE ORDER_TYPE = 'WAVE'  AND ORDER_ID = '" + orderID + "'";
                vanningInfo = vanningDA.GetVanningByWhere(where, this.whLoginID, this.pubParasInfo);
            }
            if (vanningInfo == null)
            {
                vanningInfo = new VanningInfo {
                    VanningID = string.Empty,
                    CreatedBy = this.pubParasInfo.UserID,
                    CreatedDate = DateTime.Now,
                    ExternalOrderID = "",
                    IsActive = "Y",
                    OrderID = orderID,
                    OrderType = orderType,
                    Remark = "",
                    Status = "",
                    UpdatedBy = this.pubParasInfo.UserID,
                    UpdatedDate = vanningInfo.CreatedDate,
                    VanningType = "ByPickDetail",
                    VanningDate = DateTime.Now.ToString()
                };
                string vanningID = new VanningBLL(this.pubParasInfo).InsertVanning(vanningInfo);
                if (!string.IsNullOrEmpty(vanningID))
                {
                    vanningInfo.VanningID = vanningID;
                    vanningExist = true;
                }
            }
            else
            {
                vanningExist = true;
            }
            if (!vanningExist || !(vanningInfo.VanningType == "ByPickDetail"))
            {
                throw new Exception("装箱失败：" + orderID + "已按发货明细进行装箱");
            }
            List<VanningDetailInfo> vanningDetailInfos = new List<VanningDetailInfo>();
            int maxLineID = vanningDetailDA.GetVanningDetailMaxLineID(vanningInfo.VanningID, this.whLoginID);
            pickMethod = rfPickDetailInfos[0].PickMethod;
            loadQty = rfPickDetailInfos[0].Qty;
            for (i = 0; i < rfPickDetailInfos.Count; i++)
            {
                decimal loadedVanningQty = 0M;
                decimal pickQty = 0M;
                decimal vanningQty = 0M;
                where = " WHERE PD.STATUS <> 'Shipped' AND PD.PICK_DETAIL_ID ='" + rfPickDetailInfos[i].PickDetailID + "'";
                List<RFPickDetailInfo> rfPickDetailInfoPacks = rfOutboundDA.GetRFPickDetailListInfoByWhere(where, this.whLoginID);
                if ((rfPickDetailInfoPacks != null) && (rfPickDetailInfoPacks.Count > 0))
                {
                    for (int j = 0; j < rfPickDetailInfoPacks.Count; j++)
                    {
                        loadedVanningQty += rfPickDetailInfoPacks[j].LoadQty;
                        pickQty += rfPickDetailInfoPacks[j].Qty;
                    }
                }
                if (pickMethod == "Y")
                {
                    if (loadedVanningQty > 0M)
                    {
                        throw new Exception(" 装车失败：LPN已经部分装箱");
                    }
                    vanningQty = pickQty;
                }
                else
                {
                    if (loadedVanningQty >= pickQty)
                    {
                        continue;
                    }
                    decimal lastQty = loadQty - loadedQty;
                    decimal diffQty = pickQty - loadedVanningQty;
                    if (lastQty >= diffQty)
                    {
                        loadedQty += diffQty;
                        vanningQty = diffQty;
                    }
                    else
                    {
                        loadedQty += lastQty;
                        vanningQty = lastQty;
                    }
                }
                VanningDetailInfo vanningDetailInfo = new VanningDetailInfo();
                maxLineID += 10;
                if (i == 0)
                {
                    if (string.IsNullOrEmpty(containerID))
                    {
                        vanningDetailInfo.ContainerID = "0" + (maxLineID / 10);
                    }
                    if (vanningDetailDA.CheckVanningDetailIDUnique(vanningInfo.VanningID, vanningDetailInfo.ContainerID, this.whLoginID))
                    {
                        throw new Exception("容器已经存在，请输入有效容器！");
                    }
                    containerID = vanningDetailInfo.ContainerID;
                }
                if (!containerIDGenerate)
                {
                    VanningDetailInfo vanningDetailContainerInfo = new VanningDetailInfo {
                        ContainerID = containerID,
                        BarCode = "",
                        ContainerNo = "",
                        ContainerType = "Small",
                        CreatedBy = this.pubParasInfo.UserID,
                        CreatedDate = DateTime.Now,
                        ExternLineID = "",
                        ExternOrderID = "",
                        Loc = "",
                        Lot = "",
                        LpnID = "",
                        MultiFlag = "Y",
                        OrderID = rfPickDetailInfos[i].OrderID,
                        OrderLineID = "",
                        OwnerID = "",
                        PackID = "",
                        ParentContainerID = "",
                        PickDetailID = "",
                        Qty = 0M,
                        Remark = "",
                        SealNo = "",
                        SkuID = "",
                        Status = "OK",
                        TotalQty = 0M,
                        UomID = "",
                        UpdatedBy = this.pubParasInfo.UserID,
                        UpdatedDate = DateTime.Now,
                        VannindLineID = maxLineID,
                        VanningID = vanningInfo.VanningID,
                        IsActive = "N",
                        Amount = 0M,
                        Length = 0M,
                        Width = 0M,
                        Height = 0M,
                        Volume = 0M,
                        Weight = 0M
                    };
                    vanningDetailInfos.Add(vanningDetailContainerInfo);
                    maxLineID += 10;
                    containerIDGenerate = true;
                }
                vanningDetailInfo.ContainerID = containerID;
                vanningDetailInfo.BarCode = "";
                vanningDetailInfo.ContainerNo = "";
                vanningDetailInfo.ContainerType = "Small";
                vanningDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                vanningDetailInfo.CreatedDate = DateTime.Now;
                vanningDetailInfo.ExternLineID = "";
                vanningDetailInfo.ExternOrderID = "";
                vanningDetailInfo.Loc = "";
                vanningDetailInfo.Lot = rfPickDetailInfos[i].Lot;
                vanningDetailInfo.LpnID = rfPickDetailInfos[i].LpnID;
                vanningDetailInfo.MultiFlag = "Y";
                vanningDetailInfo.OrderID = rfPickDetailInfos[i].OrderID;
                vanningDetailInfo.OrderLineID = rfPickDetailInfos[i].OrderLineID.ToString();
                vanningDetailInfo.OwnerID = rfPickDetailInfos[i].OwnerID;
                vanningDetailInfo.PackID = rfPickDetailInfos[i].PackID;
                vanningDetailInfo.ParentContainerID = "";
                vanningDetailInfo.PickDetailID = rfPickDetailInfos[i].PickDetailID;
                vanningDetailInfo.Qty = vanningQty;
                vanningDetailInfo.Remark = "";
                vanningDetailInfo.SealNo = "";
                vanningDetailInfo.SkuID = rfPickDetailInfos[i].SkuID;
                vanningDetailInfo.Status = "OK";
                vanningDetailInfo.TotalQty = rfPickDetailInfos[i].Qty;
                vanningDetailInfo.UomID = rfPickDetailInfos[i].UomID;
                vanningDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                vanningDetailInfo.UpdatedDate = DateTime.Now;
                vanningDetailInfo.VannindLineID = maxLineID;
                vanningDetailInfo.VanningID = vanningInfo.VanningID;
                vanningDetailInfo.IsActive = "Y";
                vanningDetailInfo.Amount = 0M;
                vanningDetailInfo.Length = 0M;
                vanningDetailInfo.Width = 0M;
                vanningDetailInfo.Height = 0M;
                vanningDetailInfo.Volume = 0M;
                vanningDetailInfo.Weight = 0M;
                vanningDetailInfos.Add(vanningDetailInfo);
                if ((pickMethod == "N") && (loadQty == loadedQty))
                {
                    break;
                }
            }
            if ((pickMethod == "N") && (loadQty != loadedQty))
            {
                throw new Exception("装箱失败：装箱数量大于拣货数量");
            }
            if ((vanningDetailInfos != null) && (vanningDetailInfos.Count > 0))
            {
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    for (i = 0; i < vanningDetailInfos.Count; i++)
                    {
                        result += vanningDetailDA.InsertVanningDetail(dataBase, tran, vanningDetailInfos[i], this.whLoginID);
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                }
                finally
                {
                    conn.Close();
                }
            }
            if (result == 0)
            {
                return "";
            }
            return containerID;
        }

        public DataSet GetAllAvailableSalesOrderPickInfo(string orderID)
        {
            OrdersQueryEntity ordersQuery = new OrdersQueryEntity {
                IsGetAll = true,
                SortDirection = "DESC",
                SortField = "SO.ORDER_ID"
            };
            List<string> sqlWhere = new List<string>();
            if (string.IsNullOrEmpty(orderID))
            {
                sqlWhere.Add(" O.STATUS = 'New' OR O.STATUS = 'PartPicked' OR O.STATUS = 'PartShipped' ORDER BY O.ORDER_ID DESC ");
            }
            else
            {
                sqlWhere.Add(" O.ORDER_ID = '" + orderID + "' AND (O.STATUS = 'New' OR O.STATUS = 'PartPicked' OR O.STATUS = 'PartShipped') ORDER BY O.ORDER_ID DESC ");
            }
            return ordersDA.GetOrdersByQueryList(sqlWhere, ordersQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetAllAvailableSalesOrderShipInfo(string orderID)
        {
            OrdersQueryEntity ordersQuery = new OrdersQueryEntity {
                IsGetAll = true,
                SortDirection = "DESC",
                SortField = "O.ORDER_ID"
            };
            List<string> sqlWhere = new List<string>();
            if (string.IsNullOrEmpty(orderID))
            {
                sqlWhere.Add(" O.STATUS = 'PartPicked' OR O.STATUS = 'Picked' OR O.STATUS = 'PartShipped' ORDER BY O.ORDER_ID DESC ");
            }
            else
            {
                sqlWhere.Add(" O.ORDER_ID = '" + orderID + "' AND (O.STATUS = 'PartPicked' OR O.STATUS = 'Picked' OR O.STATUS = 'PartShipped') ORDER BY O.ORDER_ID DESC ");
            }
            return ordersDA.GetOrdersByQueryList(sqlWhere, ordersQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<LoadDetailInfo> GetLoadDetailListByID(string loadID)
        {
            return loadDetailDA.GetLoadDetailByID(loadID, this.whLoginID);
        }

        public List<RFPickDetailInfo> GetLoadDetailListInfoByOrderIDAndLpn(string orderID, string lpnID, string loc, string checkPalletPick)
        {
            int i;
            List<RFPickDetailInfo> rfPickDetailInfos = new List<RFPickDetailInfo>();
            string where = string.Empty;
            string pickMethod = string.Empty;
            decimal allocatedPickQty = 0M;
            decimal invQty = 0M;
            decimal invPickedQty = 0M;
            decimal otherQty = 0M;
            where = " WHERE PD.STATUS = 'Picked' AND PD.ORDER_ID = '" + orderID + "' AND PD.LPN_ID = '" + lpnID + "' AND PD.TO_LOC = '" + loc + "'";
            List<PickDetailInfo> pickDetailInfos = pickDetailDA.GetPickDetailsByWhere(where, this.whLoginID, this.pubParasInfo);
            for (i = 0; i < pickDetailInfos.Count; i++)
            {
                allocatedPickQty += pickDetailInfos[i].Qty;
            }
            if (allocatedPickQty > 0M)
            {
                if (checkPalletPick == "Y")
                {
                    string invLoc = string.Empty;
                    List<InvLotLocLpnInfo> invLotLocLpns = invLotLocLpnDA.GetInvLotLocLpnByLpn(lpnID, this.whLoginID);
                    for (i = 0; i < invLotLocLpns.Count; i++)
                    {
                        invLoc = invLotLocLpns[0].Loc;
                        invQty += invLotLocLpns[i].Qty;
                        invPickedQty += invLotLocLpns[i].PickedQty;
                        otherQty += (((invLotLocLpns[i].AllocatedQty + invLotLocLpns[i].PickinprocessQty) + invLotLocLpns[i].ExpectedQty) + invLotLocLpns[i].PendingMoveIn) + invLotLocLpns[i].ArchiveQty;
                    }
                    if ((((allocatedPickQty > 0M) && (invQty == invPickedQty)) && ((allocatedPickQty == invQty) && (otherQty == 0M))) && (invLoc == loc))
                    {
                        pickMethod = "Y";
                    }
                    else
                    {
                        pickMethod = "N";
                    }
                }
                for (i = 0; i < pickDetailInfos.Count; i++)
                {
                    RFPickDetailInfo rfPickDetailInfo = new RFPickDetailInfo {
                        PickDetailID = pickDetailInfos[i].PickDetailID,
                        OwnerID = pickDetailInfos[i].OwnerID,
                        SkuID = pickDetailInfos[i].SkuID,
                        SkuDescr = pickDetailInfos[i].SkuDescr,
                        Qty = pickDetailInfos[i].Qty,
                        MovedQty = pickDetailInfos[i].MovedQty,
                        PickMethod = pickMethod,
                        Status = pickDetailInfos[i].Status
                    };
                    LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(pickDetailInfos[i].Lot, this.whLoginID);
                    if (lotAttributeInfo != null)
                    {
                        rfPickDetailInfo.LotExternalLot = lotAttributeInfo.ExternalLot;
                        rfPickDetailInfo.LotAttr01 = lotAttributeInfo.LotAttr01;
                    }
                    rfPickDetailInfos.Add(rfPickDetailInfo);
                }
            }
            return rfPickDetailInfos;
        }

        public DataSet GetLoadOrdersShippingInfo(string loadID)
        {
            LoadHeaderQueryEntity loadHeaderQuery = new LoadHeaderQueryEntity {
                IsGetAll = true
            };
            List<string> sqlWhere = new List<string>();
            if (string.IsNullOrEmpty(loadID))
            {
                sqlWhere.Add(" STATUS = 'New' OR STATUS = 'Loaded' ORDER BY LOAD_ID DESC ");
            }
            else
            {
                sqlWhere.Add(" LOAD_ID = '" + loadID + "' AND (STATUS = 'New' OR STATUS = 'Loaded') ORDER BY LOAD_ID DESC ");
            }
            return loadHeaderDA.GetLoadHeaderByQueryList(sqlWhere, loadHeaderQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetOrdersByPickAllocateInfo(string orderID, string where)
        {
            OrdersQueryEntity ordersQuery = new OrdersQueryEntity {
                IsGetAll = true,
                SortDirection = "DESC",
                SortField = "SO.ORDER_ID"
            };
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(where))
            {
                sqlWhere.Add(where);
            }
            if (!string.IsNullOrEmpty(orderID))
            {
                sqlWhere.Add(" O.ORDER_ID = '" + orderID + "'");
            }
            sqlWhere.Add(" (O.STATUS = 'PartAllocated' OR O.STATUS = 'Allocated' OR O.STATUS = 'PartPicked' OR O.STATUS = 'PartShipped') ORDER BY O.ORDER_ID DESC ");
            return ordersDA.GetOrdersByQueryList(sqlWhere, ordersQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetPackageShippingInfo(string orderID)
        {
            OrdersQueryEntity ordersQuery = new OrdersQueryEntity {
                IsGetAll = true,
                SortDirection = "DESC",
                SortField = "SO.ORDER_ID"
            };
            List<string> sqlWhere = new List<string>();
            if (string.IsNullOrEmpty(orderID))
            {
                sqlWhere.Add("  O.STATUS = 'PartPicked' OR O.STATUS = 'Picked' OR O.STATUS = 'PartShipped' ORDER BY O.ORDER_ID DESC ");
            }
            else
            {
                sqlWhere.Add(" O.ORDER_ID = '" + orderID + "' AND (O.STATUS = 'PartPicked' OR O.STATUS = 'Picked' OR O.STATUS = 'PartShipped') ORDER BY O.ORDER_ID DESC ");
            }
            return ordersDA.GetOrdersByQueryList(sqlWhere, ordersQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetPackageWaveInfo(string waveID)
        {
            WaveQueryEntity waveQuery = new WaveQueryEntity {
                IsGetAll = true
            };
            List<string> sqlWhere = new List<string>();
            if (string.IsNullOrEmpty(waveID))
            {
                sqlWhere.Add(" STATUS = 'PartPicked' OR STATUS = 'Picked' OR STATUS = 'PartShipped' ORDER BY WAVE_ID DESC ");
            }
            else
            {
                sqlWhere.Add(" WAVE_ID = '" + waveID + "' AND (STATUS = 'PartPicked' OR STATUS = 'Picked' OR STATUS = 'PartShipped') ORDER BY WAVE_ID DESC ");
            }
            return waveDA.GetWaveByQueryList(sqlWhere, waveQuery, this.dataBaseID[0]);
        }

        public RFPickDetailInfo GetPickDetailInfoOrderByLoc(List<string> orderIDS, RFPickDetailInfo rfPickInfo)
        {
            string where = " WHERE PD.STATUS = 'New' ";
            string orderID = string.Empty;
            for (int i = 0; i < orderIDS.Count; i++)
            {
                orderID = orderID + "'" + orderIDS[i] + "',";
            }
            orderID = orderID.Substring(0, orderID.Length - 1);
            where = where + " AND PD.ORDER_ID IN (" + orderID + ")";
            if (!string.IsNullOrEmpty(rfPickInfo.OwnerID))
            {
                where = where + " AND PD.OWNER_ID = '" + rfPickInfo.OwnerID + "'";
            }
            if (!string.IsNullOrEmpty(rfPickInfo.SkuID))
            {
                where = where + " AND PD.SKU_ID = '" + rfPickInfo.SkuID + "'";
            }
            if (!string.IsNullOrEmpty(rfPickInfo.Loc))
            {
                where = where + " AND PD.LOC = '" + rfPickInfo.Loc + "'";
            }
            if (!string.IsNullOrEmpty(rfPickInfo.LotExternalLot))
            {
                where = where + " AND LA.EXTERNAL_LOT = '" + rfPickInfo.LotExternalLot + "'";
            }
            if (!string.IsNullOrEmpty(rfPickInfo.LotAttr05))
            {
                where = where + " AND LA.LOT_ATTR05 = '" + rfPickInfo.LotAttr05 + "'";
            }
            where = where + " ORDER BY PD.SEQ_NO,PD.LOC";
            PickDetailInfo pickDetailInfo = pickDetailDA.GetPickDetailByWhere(where, this.whLoginID, this.pubParasInfo);
            RFPickDetailInfo rfPickDetailInfo = new RFPickDetailInfo();
            if (pickDetailInfo != null)
            {
                rfPickDetailInfo.PickDetailID = pickDetailInfo.PickDetailID;
                rfPickDetailInfo.OrderID = pickDetailInfo.OrderID;
                rfPickDetailInfo.OrderLineID = pickDetailInfo.OrderLineID;
                rfPickDetailInfo.OwnerID = pickDetailInfo.OwnerID;
                rfPickDetailInfo.SkuID = pickDetailInfo.SkuID;
                rfPickDetailInfo.Qty = pickDetailInfo.Qty;
                rfPickDetailInfo.Loc = pickDetailInfo.Loc;
                rfPickDetailInfo.Lot = pickDetailInfo.Lot;
                rfPickDetailInfo.PackID = pickDetailInfo.PackID;
                rfPickDetailInfo.UomID = pickDetailInfo.UomID;
                rfPickDetailInfo.Qty = pickDetailInfo.Qty;
                LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(pickDetailInfo.Lot, this.whLoginID);
                if (lotAttributeInfo != null)
                {
                    rfPickDetailInfo.LotExternalLot = lotAttributeInfo.ExternalLot;
                    rfPickDetailInfo.LotAttr01 = lotAttributeInfo.LotAttr01;
                    rfPickDetailInfo.LotAttr05 = lotAttributeInfo.LotAttr05;
                }
                return rfPickDetailInfo;
            }
            return null;
        }

        public List<RFPickDetailInfo> GetPickDetailListInfoByIDS(List<string> orderIDS, string selectAll)
        {
            List<RFPickDetailInfo> rfPickDetailInfos = new List<RFPickDetailInfo>();
            if ((orderIDS != null) && (orderIDS.Count > 0))
            {
                for (int i = 0; i < orderIDS.Count; i++)
                {
                    string where = string.Empty;
                    if (selectAll == "Y")
                    {
                        where = " WHERE PD.STATUS <> 'Shipped' AND PD.ORDER_ID = '" + orderIDS[i] + "' ORDER BY PD.LPN_ID";
                    }
                    else
                    {
                        where = " WHERE PD.STATUS <> 'Shipped' PD.QTY <> PD.LOAD_QTY AND PD.ORDER_ID = '" + orderIDS[i] + "' ORDER BY PD.LPN_ID";
                    }
                    OrdersInfo ordersInfo = ordersDA.GetOrdersByID(orderIDS[i], this.whLoginID, this.pubParasInfo);
                    List<PickDetailInfo> pickDetailInfos = pickDetailDA.GetPickDetailsByWhere(where, this.whLoginID, this.pubParasInfo);
                    if ((pickDetailInfos != null) && (pickDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < pickDetailInfos.Count; j++)
                        {
                            RFPickDetailInfo rfPickDetailInfo = new RFPickDetailInfo {
                                OrderID = pickDetailInfos[j].OrderID,
                                PickDetailID = pickDetailInfos[j].PickDetailID,
                                OwnerID = pickDetailInfos[j].OwnerID,
                                SkuID = pickDetailInfos[j].SkuID,
                                SkuDescr = pickDetailInfos[j].SkuDescr,
                                Qty = pickDetailInfos[j].Qty,
                                MovedQty = pickDetailInfos[j].MovedQty,
                                LoadQty = pickDetailInfos[j].LoadQty,
                                LpnID = pickDetailInfos[j].LpnID,
                                Loc = pickDetailInfos[j].Loc,
                                ToLoc = pickDetailInfos[j].ToLoc,
                                Status = pickDetailInfos[j].Status
                            };
                            LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(pickDetailInfos[j].Lot, this.whLoginID);
                            if (lotAttributeInfo != null)
                            {
                                rfPickDetailInfo.LotExternalLot = lotAttributeInfo.ExternalLot;
                                rfPickDetailInfo.LotAttr01 = lotAttributeInfo.LotAttr01;
                            }
                            rfPickDetailInfo.CustID = ordersInfo.ConsigneeID;
                            rfPickDetailInfo.CCompany = ordersInfo.CCompany;
                            rfPickDetailInfos.Add(rfPickDetailInfo);
                        }
                    }
                }
            }
            return rfPickDetailInfos;
        }

        public List<PickDetailInfo> GetPickDetailListInfoByLpn(string lpnID)
        {
            List<PickDetailInfo> pickDetailInfoList = new List<PickDetailInfo>();
            List<string> pickDetailID = pickDetailDA.GetPickDetailIDByLpn(lpnID, this.dataBaseID[0]);
            for (int i = 0; i < pickDetailID.Count; i++)
            {
                PickDetailInfo pickDetailInfo = pickDetailDA.GetPickDetailByID(pickDetailID[i], this.whLoginID, this.pubParasInfo);
                if (pickDetailInfo != null)
                {
                    pickDetailInfoList.Add(pickDetailInfo);
                }
            }
            return pickDetailInfoList;
        }

        public List<RFPickDetailInfo> GetPickDetailListInfoByOrderIDAndLpn(List<string> orderIDS, string lpnID, string loc, string checkPalletPick)
        {
            int i;
            List<RFPickDetailInfo> rfPickDetailInfos = new List<RFPickDetailInfo>();
            string where = string.Empty;
            string pickMethod = string.Empty;
            decimal allocatedPickQty = 0M;
            decimal invQty = 0M;
            decimal invAllocatedQty = 0M;
            decimal otherQty = 0M;
            string orderID = string.Empty;
            for (i = 0; i < orderIDS.Count; i++)
            {
                orderID = orderID + "'" + orderIDS[i] + "',";
            }
            orderID = orderID.Substring(0, orderID.Length - 1);
            where = " WHERE PD.STATUS = 'New' AND PD.ORDER_ID IN (" + orderID + ") AND PD.LPN_ID = '" + lpnID + "' AND PD.LOC = '" + loc + "'";
            List<PickDetailInfo> pickDetailInfos = pickDetailDA.GetPickDetailsByWhere(where, this.whLoginID, this.pubParasInfo);
            for (i = 0; i < pickDetailInfos.Count; i++)
            {
                allocatedPickQty += pickDetailInfos[i].Qty;
            }
            if (allocatedPickQty > 0M)
            {
                if (checkPalletPick == "Y")
                {
                    string invLoc = string.Empty;
                    List<InvLotLocLpnInfo> invLotLocLpns = invLotLocLpnDA.GetInvLotLocLpnByLpn(lpnID, this.whLoginID);
                    for (i = 0; i < invLotLocLpns.Count; i++)
                    {
                        invLoc = invLotLocLpns[0].Loc;
                        invQty += invLotLocLpns[i].Qty;
                        invAllocatedQty += invLotLocLpns[i].AllocatedQty;
                        otherQty += (((invLotLocLpns[i].PickedQty + invLotLocLpns[i].PickinprocessQty) + invLotLocLpns[i].ExpectedQty) + invLotLocLpns[i].PendingMoveIn) + invLotLocLpns[i].ArchiveQty;
                    }
                    if ((((allocatedPickQty > 0M) && (invQty == invAllocatedQty)) && ((allocatedPickQty == invQty) && (otherQty == 0M))) && (invLoc.ToUpper() == loc.ToUpper()))
                    {
                        pickMethod = "Y";
                    }
                    else
                    {
                        pickMethod = "N";
                    }
                }
                for (i = 0; i < pickDetailInfos.Count; i++)
                {
                    RFPickDetailInfo rfPickDetailInfo = new RFPickDetailInfo {
                        PickDetailID = pickDetailInfos[i].PickDetailID,
                        OwnerID = pickDetailInfos[i].OwnerID,
                        SkuID = pickDetailInfos[i].SkuID,
                        SkuDescr = pickDetailInfos[i].SkuDescr,
                        Qty = pickDetailInfos[i].Qty,
                        MovedQty = pickDetailInfos[i].MovedQty,
                        PickMethod = pickMethod
                    };
                    LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(pickDetailInfos[i].Lot, this.whLoginID);
                    if (lotAttributeInfo != null)
                    {
                        rfPickDetailInfo.LotExternalLot = lotAttributeInfo.ExternalLot;
                        rfPickDetailInfo.LotAttr01 = lotAttributeInfo.LotAttr01;
                        rfPickDetailInfo.LotAttr02 = lotAttributeInfo.LotAttr02;
                        rfPickDetailInfo.LotAttr03 = lotAttributeInfo.LotAttr03;
                        rfPickDetailInfo.LotAttr04 = lotAttributeInfo.LotAttr04;
                        rfPickDetailInfo.LotAttr05 = lotAttributeInfo.LotAttr05;
                        rfPickDetailInfo.LotAttr06 = lotAttributeInfo.LotAttr06;
                        rfPickDetailInfo.LotAttr07 = lotAttributeInfo.LotAttr07;
                        rfPickDetailInfo.LotAttr08 = lotAttributeInfo.LotAttr08;
                        rfPickDetailInfo.LotAttr09 = lotAttributeInfo.LotAttr09;
                        rfPickDetailInfo.LotAttr10 = lotAttributeInfo.LotAttr10;
                        rfPickDetailInfo.LotProduceDate = lotAttributeInfo.ProduceDate;
                        rfPickDetailInfo.LotExpiryDate = lotAttributeInfo.ExpiryDate;
                        rfPickDetailInfo.LotVendorID = lotAttributeInfo.VendorID;
                    }
                    rfPickDetailInfos.Add(rfPickDetailInfo);
                }
            }
            return rfPickDetailInfos;
        }

        public List<RFPickDetailInfo> GetPickDetailListInfoByPack(List<string> orderIDS)
        {
            string orderID = string.Empty;
            for (int i = 0; i < orderIDS.Count; i++)
            {
                orderID = orderID + "'" + orderIDS[i] + "',";
            }
            orderID = orderID.Substring(0, orderID.Length - 1);
            string where = " WHERE PD.STATUS <> 'Shipped' AND PD.ORDER_ID IN (" + orderID + ")";
            return rfOutboundDA.GetRFPickDetailListInfoByWhere(where, this.whLoginID);
        }

        public string GetRotationRuleByPickInfo(string lpnID, string waveID, string orderID)
        {
            decimal orderQty = 0M;
            decimal lpnQty = 0M;
            decimal availableQty = 0M;
            bool lotExist = false;
            string error = string.Empty;
            string skuRotation = string.Empty;
            List<string> lot = new List<string>();
            InventoryInfo inventoryInfo = InventoryMgtDA.GetInventoryInfoByLpnID(lpnID, this.whLoginID);
            if (inventoryInfo != null)
            {
                int i;
                string where;
                List<OrderDetailInfo> orderDetailList;
                if (!string.IsNullOrEmpty(waveID))
                {
                    List<WaveDetailInfo> waveDetailInfoList = waveDetailDA.GetWaveDetailListByID(waveID, this.whLoginID);
                    if ((waveDetailInfoList != null) && (waveDetailInfoList.Count > 0))
                    {
                        for (i = 0; i < waveDetailInfoList.Count; i++)
                        {
                            if ((waveDetailInfoList[i].ProcessStatus.ToUpper() != "SHIPPED") && (waveDetailInfoList[i].ProcessStatus.ToUpper() != "PICKED"))
                            {
                                where = " AND OD.OWNER_ID = '" + inventoryInfo.OwnerID + "' AND OD.SKU_ID = '" + inventoryInfo.SkuID + "' AND OD.STATUS IN ('New','PartPicked','PartShipped')  AND (OD.LOT_ATTR09 = '' OR OD.LOT_ATTR09 = '" + inventoryInfo.LotAttr09 + "' ) AND (OD.LOT_ATTR01 = '' OR OD.LOT_ATTR01 = '" + inventoryInfo.LotAttr01 + "' )";
                                orderDetailList = orderDetailDA.GetOrderDetailListByOrderID(waveDetailInfoList[i].OrderID, where, this.whLoginID, this.pubParasInfo);
                                for (int j = 0; j < orderDetailList.Count; j++)
                                {
                                    orderQty = (orderQty + orderDetailList[j].OriginalQty) - orderDetailList[j].PickedQty;
                                    skuRotation = orderDetailList[j].SkuRotation;
                                }
                                orderDetailList.Clear();
                            }
                        }
                    }
                }
                else
                {
                    where = " AND OD.OWNER_ID = '" + inventoryInfo.OwnerID + "' AND OD.SKU_ID = '" + inventoryInfo.SkuID + "' AND OD.STATUS IN ('New','PartPicked','PartShipped')  AND (OD.LOT_ATTR09 = '' OR OD.LOT_ATTR09 = '" + inventoryInfo.LotAttr09 + "' ) AND (OD.LOT_ATTR01 = '' OR OD.LOT_ATTR01 = '" + inventoryInfo.LotAttr01 + "' )";
                    orderDetailList = orderDetailDA.GetOrderDetailListByOrderID(orderID, where, this.whLoginID, this.pubParasInfo);
                    for (i = 0; i < orderDetailList.Count; i++)
                    {
                        orderQty = (orderQty + orderDetailList[i].OriginalQty) - orderDetailList[i].PickedQty;
                        skuRotation = orderDetailList[i].SkuRotation;
                    }
                }
                if (string.IsNullOrEmpty(skuRotation) || (orderQty <= 0M))
                {
                    return error;
                }
                OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL(this.pubParasInfo);
                RotationRuleInfo rotationRuleInfo = rotataionRuleDA.GetRotationRuleByID(skuRotation, this.whLoginID);
                rotationRuleInfo.RotationRuleDetailInfo = rotataionRuleDetailDA.GetRotationRuleDetailListByID(skuRotation, this.whLoginID);
                if (rotationRuleInfo == null)
                {
                    return error;
                }
                where = " AND ILLL.SKU_ID ='" + inventoryInfo.SkuID + "' AND ILLL.OWNER_ID = '" + inventoryInfo.OwnerID + "' AND LA.LOT_ATTR09 = '" + inventoryInfo.LotAttr09 + "' AND LA.LOT_ATTR01 = '" + inventoryInfo.LotAttr01 + "'";
                string sqlOrderBy = outBoundMgtBll.OrganizeRotationRule(rotationRuleInfo);
                where = where + " " + sqlOrderBy;
                List<InventoryInfo> inventoryInfoList = InventoryMgtDA.GetInventoryInfoByWhere(where, this.whLoginID);
                if ((inventoryInfoList == null) || (inventoryInfoList.Count <= 0))
                {
                    return error;
                }
                for (i = 0; i < inventoryInfoList.Count; i++)
                {
                    if (lpnQty >= orderQty)
                    {
                        break;
                    }
                    availableQty = ((((inventoryInfoList[i].Qty - inventoryInfoList[i].AllocatedQty) - inventoryInfoList[i].ArchiveQty) - inventoryInfoList[i].ExpectedQty) - inventoryInfoList[i].PendingMoveIn) - inventoryInfoList[i].PickedQty;
                    if (availableQty > 0M)
                    {
                        lpnQty += availableQty;
                        lot.Add(inventoryInfoList[i].Lot);
                        if (inventoryInfo.Lot == inventoryInfoList[i].Lot)
                        {
                            lotExist = true;
                            if (((lot.Count >= 2) && (availableQty >= orderQty)) && (lot[lot.Count - 1] != lot[lot.Count - 2]))
                            {
                                error = PublicMethod.GetInstance().RF_ShowAlertMessage("库存中有更早的批次，是否继续？;庫存中有更早的批次，是否繼續？;Inventory exist prior Lot,go on？", this.pubParasInfo.UserLanguage);
                            }
                        }
                    }
                }
                if (!((lot.Count <= 0) || lotExist))
                {
                    error = PublicMethod.GetInstance().RF_ShowAlertMessage("库存中有更早的批次，是否继续？;庫存中有更早的批次，是否繼續？;Inventory exist prior Lot,go on？", this.pubParasInfo.UserLanguage);
                }
            }
            return error;
        }

        public DataSet GetWaveByQueryListPickInfo(string waveID, string where)
        {
            WaveQueryEntity waveQuery = new WaveQueryEntity {
                IsGetAll = true
            };
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(waveID))
            {
                sqlWhere.Add(" WAVE_ID = '" + waveID + "'");
            }
            if (string.IsNullOrEmpty(where))
            {
                sqlWhere.Add(" (STATUS = 'PartAllocated' OR STATUS = 'Allocated' OR STATUS = 'PartPicked' OR STATUS = 'PartShipped') ORDER BY WAVE_ID DESC ");
            }
            else
            {
                sqlWhere.Add(where);
            }
            return waveDA.GetWaveByQueryList(sqlWhere, waveQuery, this.dataBaseID[0]);
        }

        public DataSet GetWaveByQueryListShippingInfo(string waveID)
        {
            WaveQueryEntity waveQuery = new WaveQueryEntity {
                IsGetAll = true
            };
            List<string> sqlWhere = new List<string>();
            if (string.IsNullOrEmpty(waveID))
            {
                sqlWhere.Add(" STATUS = 'PartPicked' OR STATUS = 'Picked' OR STATUS = 'PartShipped' ORDER BY WAVE_ID DESC ");
            }
            else
            {
                sqlWhere.Add(" WAVE_ID = '" + waveID + "' AND (STATUS = 'PartPicked' OR STATUS = 'Picked' OR STATUS = 'PartShipped') ORDER BY WAVE_ID DESC ");
            }
            return waveDA.GetWaveByQueryList(sqlWhere, waveQuery, this.dataBaseID[0]);
        }

        public List<WaveDetailInfo> GetWaveDetailListByID(string waveID)
        {
            return waveDetailDA.GetWaveDetailListByID(waveID, this.whLoginID);
        }

        public string SaveLoadDetailByPickDetails(string loadID, List<RFPickDetailInfo> rfPickDetailInfos)
        {
            int result = 0;
            string pickMethod = string.Empty;
            decimal loadQty = 0M;
            decimal loadedQty = 0M;
            int maxLineID = loadPickDetailDA.GetLoadPickDetailMaxLineID(loadID, this.whLoginID);
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if ((rfPickDetailInfos != null) && (rfPickDetailInfos.Count > 0))
                {
                    pickMethod = rfPickDetailInfos[0].PickMethod;
                    loadQty = rfPickDetailInfos[0].LoadQty;
                    for (i = 0; i < rfPickDetailInfos.Count; i++)
                    {
                        PickDetailInfo pickDetailInfo = pickDetailDA.GetPickDetailByID(dataBase, tran, rfPickDetailInfos[i].PickDetailID, this.whLoginID, this.pubParasInfo);
                        if (pickMethod == "Y")
                        {
                            if (pickDetailInfo.LoadQty > 0M)
                            {
                                throw new Exception(" 装车失败：LPN已经部分装车");
                            }
                            pickDetailInfo.LoadQty += rfPickDetailInfos[i].Qty;
                        }
                        else
                        {
                            if (!(pickDetailInfo.LoadQty != pickDetailInfo.Qty))
                            {
                                continue;
                            }
                            decimal lastQty = loadQty - loadedQty;
                            decimal diffQty = pickDetailInfo.Qty - pickDetailInfo.LoadQty;
                            if (lastQty >= diffQty)
                            {
                                loadedQty += diffQty;
                                pickDetailInfo.LoadQty += diffQty;
                            }
                            else
                            {
                                pickDetailInfo.LoadQty += lastQty;
                                loadedQty += lastQty;
                            }
                        }
                        pickDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                        pickDetailInfo.UpdatedDate = DateTime.Now;
                        result += pickDetailDA.UpdatePickDetail(dataBase, tran, pickDetailInfo, this.whLoginID);
                        if (pickDetailInfo.Qty == pickDetailInfo.LoadQty)
                        {
                            LoadPickDetailInfo loadPickDetailInfo;
                            maxLineID += 10;
                            loadPickDetailInfo = new LoadPickDetailInfo();
                            loadPickDetailInfo.LoadID = loadID;
                            loadPickDetailInfo.LineID = maxLineID;
                            loadPickDetailInfo.PickDetailID = pickDetailInfo.PickDetailID;
                            loadPickDetailInfo.Status = pickDetailInfo.Status;
                            loadPickDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                            loadPickDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                            loadPickDetailInfo.CreatedDate = DateTime.Now;
                            loadPickDetailInfo.UpdatedDate = loadPickDetailInfo.CreatedDate;
                            loadPickDetailDA.InsertLoadPickDetail(dataBase, tran, loadPickDetailInfo, this.dataBaseID);
                        }
                        if ((pickMethod == "N") && (loadQty == loadedQty))
                        {
                            break;
                        }
                    }
                    if ((pickMethod == "N") && (loadQty != loadedQty))
                    {
                        throw new Exception("装车失败：装车数量大于拣货数量");
                    }
                }
                int loadPickCount = 0;
                List<LoadDetailInfo> loadDetailInfos = loadDetailDA.GetLoadDetailByID(dataBase, tran, loadID, this.whLoginID);
                if ((loadDetailInfos != null) && (loadDetailInfos.Count > 0))
                {
                    for (i = 0; i < loadDetailInfos.Count; i++)
                    {
                        string where = " WHERE PD.QTY <> PD.LOAD_QTY AND PD.ORDER_ID = '" + loadDetailInfos[i].OrderID + "' ORDER BY PD.LPN_ID";
                        List<PickDetailInfo> pickDetailInfos = pickDetailDA.GetPickDetailsByWhere(dataBase, tran, where, this.whLoginID, this.pubParasInfo);
                        if ((pickDetailInfos != null) && (pickDetailInfos.Count > 0))
                        {
                            loadPickCount += pickDetailInfos.Count;
                        }
                    }
                    if (loadPickCount == 0)
                    {
                        loadHeaderDA.UpdateLoadAndOrderStatusByLoadID(dataBase, tran, "Loaded", loadID, this.pubParasInfo.UserID, this.whLoginID);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (result == 0)
            {
                return "";
            }
            return result.ToString();
        }

        public string ShippedByLoadID(string loadID)
        {
            string result = string.Empty;
            List<string> orderID = new List<string>();
            List<LoadDetailInfo> loadDetailInfos = loadDetailDA.GetLoadDetailByID(loadID, this.whLoginID);
            if ((loadDetailInfos != null) && (loadDetailInfos.Count > 0))
            {
                for (int i = 0; i < loadDetailInfos.Count; i++)
                {
                    orderID.Add(loadDetailInfos[i].OrderID);
                }
                result = this.ShippedByOrderID(orderID);
                if (string.IsNullOrEmpty(result))
                {
                    return result;
                }
                LoadHeaderInfo loadHeaderInfo = loadHeaderDA.GetLoadHeaderByID(loadID, this.whLoginID);
                if (loadHeaderInfo != null)
                {
                    loadHeaderInfo.Status = "Shipped";
                    loadHeaderInfo.UpdatedBy = this.pubParasInfo.UserID;
                    loadHeaderInfo.UpdatedDate = DateTime.Now;
                    string temp = loadHeaderDA.UpdateLoadAndOrderStatusByLoadID("Shipped", loadID, this.pubParasInfo.UserID, this.whLoginID);
                }
            }
            return result;
        }

        public string ShippedByLpnID(string lpnID)
        {
            string errorInfo = string.Empty;
            bool lpnShipped = true;
            List<string> pickDetailID = pickDetailDA.GetPickDetailIDByLpn(lpnID, this.dataBaseID[0]);
            if (pickDetailID.Count > 0)
            {
                for (int i = 0; i < pickDetailID.Count; i++)
                {
                    if (pickDetailDA.GetPickDetailByID(pickDetailID[i], this.whLoginID, this.pubParasInfo).Status.ToUpper() != "SHIPPED")
                    {
                        lpnShipped = false;
                    }
                }
                if (lpnShipped)
                {
                    throw new Exception(PublicMethod.GetInstance().RF_ShowAlertMessage("LPN已发货;LPN已發貨;LPN is already Shipped", this.pubParasInfo.UserLanguage));
                }
                OutboundMgtBLL outboundMgtBll = new OutboundMgtBLL(this.pubParasInfo);
                return outboundMgtBll.ConfirmShipped(pickDetailID, "", this.pubParasInfo.UserID);
            }
            InventoryInfo inventoryInfo = InventoryMgtDA.GetInventoryInfoByLpnID(lpnID, this.whLoginID);
            if (inventoryInfo != null)
            {
                if (inventoryInfo.PickedQty == 0M)
                {
                    errorInfo = PublicMethod.GetInstance().RF_ShowAlertMessage("扫描LPN还未拣货;掃描LPN還未揀貨;LPN have not pick", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    errorInfo = PublicMethod.GetInstance().RF_ShowAlertMessage("拣货明细无此LPN;揀貨明細無此LPN;LPN is not exist", this.pubParasInfo.UserLanguage);
                }
            }
            else
            {
                errorInfo = PublicMethod.GetInstance().RF_ShowAlertMessage("扫描LPN有误;掃描LPN有誤;LPN is invalid", this.pubParasInfo.UserLanguage);
            }
            throw new Exception(errorInfo);
        }

        public string ShippedByOrderID(List<string> orderID)
        {
            OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL(this.pubParasInfo);
            return outBoundMgtBll.ShippedByOrderID(orderID, "", this.pubParasInfo.UserID);
        }

        public string UpdatePickSeqNoByLoc(List<string> orderIDS, string loc)
        {
            int i;
            int result = 0;
            string orderID = string.Empty;
            for (i = 0; i < orderIDS.Count; i++)
            {
                orderID = orderID + "'" + orderIDS[i] + "',";
            }
            orderID = orderID.Substring(0, orderID.Length - 1);
            string where = " WHERE PD.STATUS = 'New' AND PD.ORDER_ID IN (" + orderID + ") AND PD.LOC = '" + loc + "' ORDER BY PD.SEQ_NO,PD.LOC";
            List<PickDetailInfo> pickDetailInfos = pickDetailDA.GetPickDetailsByWhere(where, this.whLoginID, this.pubParasInfo);
            if ((pickDetailInfos != null) && (pickDetailInfos.Count > 0))
            {
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    for (i = 0; i < pickDetailInfos.Count; i++)
                    {
                        PickDetailInfo local1 = pickDetailInfos[i];
                        local1.SeqNo++;
                        result += pickDetailDA.UpdatePickDetail(pickDetailInfos[i], this.whLoginID);
                    }
                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return result.ToString();
        }
    }
}

